package com.gep.controlador;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Window;

import com.gep.controlador.util.Mensagem;

public class ControladorNotaZul extends Window {

	private static final long serialVersionUID = 1L;

	public Listbox listBoxItens;

	public void carregarLista() {
		try {
			try {
				HttpServletRequest request = (HttpServletRequest) Executions
						.getCurrent().getNativeRequest();
				HttpSession session = request.getSession(false);
				List listaItens = (List) session.getAttribute("listaNota");
				if (listaItens != null) {
					Listbox listbox = (Listbox) this.getFellow("listBoxItens");
					atualizarListBox(listbox, listaItens);
					Clients.print();
				} else {
					Executions.sendRedirect("/zul/home.zul");
				}
			} catch (Exception e) {
				Mensagem.EXIBIR("Erro de conexao", "ERRO");
			}

		} catch (Exception e) {

		}
	}

	@SuppressWarnings("unchecked")
	private void atualizarListBox(Listbox listbox, List listaItens) {
		ListModelList model = null;
		List items = listaItens;
		if (model == null) {
			model = new ListModelList(items.size());
			listbox.setModel(model);
		}
		model.addAll(items);

		listbox.setVisible(true);
	}

}
